﻿using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;
using Yq.BookList.Blog.Dtos;

namespace Yq.BookList.Blog
{
    /// <summary>
    /// 
    /// </summary>
    public class PostAppService : BookListAppServiceBase, IPostAppService
    {
        private readonly IRepository<Post, int> _postRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="postRepository"></param>
        public PostAppService(IRepository<Post, int> postRepository)
        {
            _postRepository = postRepository;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task BatchDeletePost(List<int> input)
        {
            await _postRepository.DeleteAsync(a => input.Contains(a.Id));
        }

        /// <summary>
        /// 批量显示或隐藏
        /// </summary>
        /// <param name="inputs"></param>
        /// <returns></returns>
        public async Task BatchShowOrHidePost(List<ShowOrHidePostDto> inputs)
        {
            foreach (var itemInput in inputs)
            {
                await UpdatePost(itemInput, itemInput.Id);
            }
        }

        /// <summary>
        /// 新增或修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrUpdatePost(CreateOrUpdatePostInput input)
        {
            if (input.Post.Id.HasValue)
            {
                //修改
                await UpdatePost(input.Post, input.Post.Id);
            }
            else
            {
                //添加
                await CreatePost(input.Post);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task DeletePost(EntityDto<int> input)
        {
            await _postRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 获取Post
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<GetPostForEditOutput> GetForEditPost(NullableIdDto<int> input)
        {
            var output = new GetPostForEditOutput();

            PostEditDto dto;

            if (input.Id.HasValue)
            {
                var entity = await _postRepository.GetAsync(input.Id.Value);

                dto = entity.MapTo<PostEditDto>();

            }
            else
            {
                dto = new PostEditDto();
            }
            output.Post = dto;
            return output;
        }

        /// <summary>
        /// 分页获取Post
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PostListDto>> GetPagePost(GetPostInput input)
        {
            var query = _postRepository.GetAll().AsNoTracking().WhereIf(
                !input.FilterText.IsNullOrWhiteSpace(), a => a.Title.Contains(input.FilterText)
            ).WhereIf(true, b => b.IsShow);
            var count = await query.CountAsync();

            var entityList = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            //autoMapper
            var entityListDtos = entityList.MapTo<List<PostListDto>>();

            //定义AutoMapper

            return new PagedResultDto<PostListDto>(count, entityListDtos);
        }

        /// <summary>
        /// 显示或隐藏
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task ShowOrHidePost(ShowOrHidePostDto input)
        {
            await UpdatePost(input, input.Id);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        protected virtual async Task<PostEditDto> CreatePost(PostEditDto input)
        {
            var entity = input.MapTo<Post>();
            await _postRepository.InsertAsync(entity);
            var dto = entity.MapTo<PostEditDto>();
            return dto;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        protected virtual async Task UpdatePost<T>(T input,int? id)
        {
            var entity = await _postRepository.GetAsync(id.Value);
            input.MapTo(entity);
            await _postRepository.UpdateAsync(entity);

        }


    }
}